package cn.congzhou.crm.common.vo;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;


@Getter@Setter
@ToString
@NoArgsConstructor
public class CommonResultJson<T> {

    /**
     * 调用成功标志
     */
    private boolean success = true;
    /**
     * 状态码
     */
    private int code = ResultCode.SUCCESS.getCode();
    /**
     * 提示信息（一般多数为错误信息）
     */
    private String msg;
    /**
     * 返回的数据(一般为调用成功返回的数据)
     */
    private T data;


    public CommonResultJson(int code,T data){
        this.code = code;
        this.data = data;
    }
    public CommonResultJson(int code,String msg,T data){
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public CommonResultJson(int code,boolean success,String msg){
        this.code = code;
        this.success = success;
        this.msg = msg;
    }

    /**
     * 返回业务成功数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> success(T data){
        return new CommonResultJson<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }


    /**
     * 返回业务成功数据，带提示信息
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> success(String msg,T data){
        return new CommonResultJson<T>(ResultCode.SUCCESS.getCode(),msg,data);
    }

    /**
     * 调用失败，返回默认消息
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> failed(){
        return new CommonResultJson<>(ResultCode.FAILED.getCode(),false,ResultCode.FAILED.getMessage());
    }


    /**
     * 调用失败，返回失败提示消息
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> failed(String msg){
        return new CommonResultJson<>(ResultCode.FAILED.getCode(),false,msg);
    }

    /**
     * 调用失败，返回失败提示消息，自定义指定code状态码
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> failed(int code,String msg){
        return new CommonResultJson<>(code,false,msg);
    }

    /**
     * 参数校验失败
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> validateFailed() {
        return new CommonResultJson<>(ResultCode.VALIDATE_FAILED.getCode(),false,ResultCode.VALIDATE_FAILED.getMessage());
    }

    /**
     * 参数校验失败，返回自定义提示消息
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> validateFailed(String msg) {
        return new CommonResultJson<>(ResultCode.VALIDATE_FAILED.getCode(),false,msg);
    }


    /**
     * 没有权限操作
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> forbidden(){
        return new CommonResultJson<>(ResultCode.FORBIDDEN.getCode(),false,ResultCode.FORBIDDEN.getMessage());
    }
    /**
     * 没有权限操作，返回自定义提示消息
     * @param <T>
     * @return
     */
    public static <T> CommonResultJson<T> forbidden(String msg){
        return new CommonResultJson<>(ResultCode.FORBIDDEN.getCode(),false,msg);
    }

    /**
     * 枚举了一些常用API操作码
     */
    public enum ResultCode {
        SUCCESS(200, "操作成功"),
        FAILED(500, "操作失败"),
        VALIDATE_FAILED(404, "参数检验失败"),
        UNAUTHORIZED(401, "暂未登录或登录已经过期"),
        FORBIDDEN(403, "没有相关权限执行此操作");
        private int code;
        private String message;

        ResultCode(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }

}
